package com.kakaxi.mybilibili.module.home.live.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.adapter.LiveHomeRecyAdapter;
import com.kakaxi.mybilibili.bean.LiveAreaCategoryItemBean;
import com.kakaxi.mybilibili.bean.BannerItemBean;
import com.kakaxi.mybilibili.bean.LiveItemBean;
import com.kakaxi.mybilibili.bean.PartitionItemBean;
import com.kakaxi.mybilibili.bean.RecyMultiItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.home.live.model.ILiveHomeModel;
import com.kakaxi.mybilibili.module.home.live.model.LiveHomeModel;
import com.kakaxi.mybilibili.module.home.live.view.ILiveHomeView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播主页面控制层实现类
 * Created by kakaxi on 16/9/30.
 */

public class LiveHomePresenter implements ILiveHomePresenter {

    /**
     * 直播视图层
     */
    private ILiveHomeView mLiveView;

    /**
     * 直播数据层
     */
    private ILiveHomeModel mLiveModel;

    public LiveHomePresenter(ILiveHomeView liveView) {
        mLiveView = liveView;
        mLiveModel = new LiveHomeModel();
    }

    @Override
    public void initLiveData() {
        getLiveData(false);
    }

    @Override
    public void refreshLiveData() {
        getLiveData(true);
    }

    /**
     * 获取直播主页面的列表数据
     *
     * @param updateCached 是否更新缓存
     */
    private void getLiveData(boolean updateCached) {
        // 调用数据层, 获取数据
        mLiveModel.getLiveData(updateCached, new Listener<JSONObject>() {

            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                // 判断是否正确
                if (response.getString(WebParamConst.RESPONSE_CODE).equals
                        (WebParamConst.RESULT_CODE_OK)) {
                    // 获取data的json对象, 并调用成功请求的回调
                    List<RecyMultiItemBean> dataList = toLiveDataList(response.getJSONObject
                            (WebParamConst.RESPONSE_DATA));
                    // 调用视图层,设置直播数据
                    mLiveView.showLiveData(dataList);
                } else {
                    // 调用显示失败的响应结果
                    mLiveView.showLiveFailed(response.getString(WebParamConst
                            .RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mLiveView.showLiveError();
            }
        });

        // 获取全部分类数据
        mLiveModel.getLiveAllCategoryData(new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                // 判断是否正确
                if (response.getString(WebParamConst.RESPONSE_CODE).equals
                        (WebParamConst.RESULT_CODE_OK)) {
                    String liveAllCategoryArrStr = response.getJSONArray(WebParamConst
                            .RESPONSE_DATA).toJSONString();
                    List<LiveAreaCategoryItemBean> areaCategoryItemBeanList = JSON.parseArray
                            (liveAllCategoryArrStr, LiveAreaCategoryItemBean.class);
                    mLiveView.setLiveAllCategoryData(areaCategoryItemBeanList);
                } else {
                    mLiveView.showLiveFailed(response.getString(WebParamConst.RESPONSE_MESSAGE));
                }
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
            }
        });
    }


    /**
     * 把直播的json数据转换为列表
     *
     * @param dataJsonObj 直播json数据
     * @return
     */
    private List<RecyMultiItemBean> toLiveDataList(JSONObject dataJsonObj) {
        // 创建存放直播各种类型数据的列表
        List<RecyMultiItemBean> finalDataList = new ArrayList<>();

        // 获取广告数据的json字符串, 并转为数组, 添加到最终数据列表中
        String bannerJsonStr = dataJsonObj.getJSONArray(WebParamConst
                .RESPONSE_DATA_BANNER).toString();
//        LogUtil.d(bannerJsonStr);
        List<BannerItemBean> bannerList = JSON.parseArray
                (bannerJsonStr, BannerItemBean.class);
        finalDataList.add(new RecyMultiItemBean(LiveHomeRecyAdapter.VIEW_TYPE_BANNER, bannerList));
        // 添加分类
        finalDataList.add(new RecyMultiItemBean(LiveHomeRecyAdapter.VIEW_TYPE_CATEGORY_ICON, null));
        // 获取推荐直播的数据并转换,最后添加到最终数据列表中
        JSONObject recommendDataJsonObj = dataJsonObj.getJSONObject(WebParamConst
                .RESPONSE_DATA_LIVE_RECOMMEND_DATA);
//        LogUtil.d(recommendDataJsonObj.getString(WebParamConst.RESPONSE_DATA_LIVE_PARTITION));
        PartitionItemBean partitionJsonObj = recommendDataJsonObj.getObject
                (WebParamConst.RESPONSE_DATA_LIVE_PARTITION, PartitionItemBean.class);
        finalDataList.add(new RecyMultiItemBean(LiveHomeRecyAdapter.VIEW_TYPE_PARTITION,
                partitionJsonObj));
        String livesJsonStr = recommendDataJsonObj.getJSONArray(WebParamConst
                .RESPONSE_DATA_LIVE_LIVES).toString();
//        LogUtil.d(livesJsonStr);
        List<LiveItemBean> livesList = JSON.parseArray(livesJsonStr, LiveItemBean.class);
        for (LiveItemBean itemBean : livesList) {
            finalDataList.add(new RecyMultiItemBean(LiveHomeRecyAdapter.VIEW_TYPE_CONTENT,
                    itemBean));
        }
        // 获取各类直播数据json数组, 并循环处理
        JSONArray partitionsJsonArr = dataJsonObj.getJSONArray(WebParamConst
                .RESPONSE_DATA_LIVE_PARTITIONS);
        for (int i = 0; i < partitionsJsonArr.size(); i++) {
            // 获取各类的直播数据并转换,最后添加到最终数据列表中
            JSONObject tempJsonObj = partitionsJsonArr.getJSONObject(i);
            partitionJsonObj = tempJsonObj.getObject
                    (WebParamConst.RESPONSE_DATA_LIVE_PARTITION, PartitionItemBean.class);
            //  LogUtil.d(tempJsonObj.getString(WebParamConst.RESPONSE_DATA_LIVE_PARTITION));
            finalDataList.add(new RecyMultiItemBean(LiveHomeRecyAdapter.VIEW_TYPE_PARTITION,
                    partitionJsonObj));
            livesJsonStr = tempJsonObj.getJSONArray(WebParamConst
                    .RESPONSE_DATA_LIVE_LIVES).toString();
            // LogUtil.d(livesJsonStr);
            livesList = JSON.parseArray(livesJsonStr, LiveItemBean.class);
            for (LiveItemBean itemBean : livesList) {
                finalDataList.add(new RecyMultiItemBean(LiveHomeRecyAdapter.VIEW_TYPE_CONTENT,
                        itemBean));
            }
        }

        return finalDataList;
    }

}
